import { observable, action } from 'mobx';
import { WeaForm } from 'comsMobx';

export interface AdvancedStoreProps {
    visible: boolean;
    form: any;
    templates: any[];
    isInit: boolean;
    update: (params:any) => void;
    updateField: (dataSource:any) => void;
}

export default class AdvancedStore implements AdvancedStoreProps {
    @observable visible = true;
    @observable form = new WeaForm();
    @observable templates = [] as any[];
    @observable isInit = false;
    @action
    update = (param: any) => {
        for (let key in param) {
            this[key] = param[key]
        }
    }
    @action
    updateField = (dataSource: any) => {
        if (!this.form.isFormInit) {
            setTimeout(() => {
                this.updateField(dataSource);
            }, 200);
            return;
        }


        let otherData={};
        for (let field in dataSource) {
            if (this.form.has(field)) {
                if(Array.isArray(dataSource[field])){
                    const _field = this.form.select(field)
                    _field.set(dataSource[field]);
                }else{
                    otherData[field]=dataSource[field];
                }
            }
        }
        this.form.updateFields(otherData,false);

    }
}